/**
 * We shall say that an n-digit number is pandigital 
 * if it makes use of all the digits 1 to n exactly once. 
 * For example, 2143 is a 4-digit pandigital and is also prime. 
 * What is the largest n-digit pandigital prime that exists?
 * Conclusion: 
 * 1. Max 8 digits, no 3 - 6 digit
 * 2. No 1, 2
 * 3. From 4, 5, 7, 8 digit
 * 4. Check from 8 digits down to 4 digits
 */

/**
 * @author TrinhNX
 * @start_date	: 2015/05/07
 * @end_date 	:
 */
public class Euler041 {
	public static void main(String[] args) {
		int[] SAMPLE = { 8, 7, 6, 5, 4, 3, 2 };
		int VALID = 1;
		int i;
		while (true) {
			i = SAMPLE.length - 1;
			Common.printArray(SAMPLE, "\t");
			System.out.println("\n");
			while (SAMPLE[i] > SAMPLE[i - 1]) {
				i--;
			}
			System.out.println("Index: " + i);
			if (i == 1) {
				Common.printArray(SAMPLE, "\t");
				System.out.println("\n");
				break;
			} else if (6 == i) {
				int temp = SAMPLE[i];
				SAMPLE[i] = SAMPLE[i - 1];
				SAMPLE[i - 1] = temp;
			} else {
				// Find the smallest number that bigger than a[i]
				int k;
				for (k = 6; k > i; k--) {
					if (SAMPLE[k] > SAMPLE[i])
						break;
				}
				Common.swap(SAMPLE, i, k);
				// Swap
				for (int j = i - 1; j < 6; j++) {
					for (int m = i; m < 6; m++) {
						if (SAMPLE[j] > SAMPLE[m]) {
							int temp = SAMPLE[j];
							SAMPLE[j] = SAMPLE[m];
							SAMPLE[m] = temp;
						}
					}
				}
			}
		}
	}
}
